import java.util.ArrayList;

import info.gridworld.actor.Actor;
import info.gridworld.actor.ActorWorld;
import info.gridworld.actor.Rock;
import info.gridworld.grid.BoundedGrid;
import info.gridworld.grid.Location;


public class Map extends ActorWorld
{
    final private static int row = 6; // row must always be even number

    final private static int col = 50;

    private int level;

    private PlayerCharacter player;

    private Combat c;


    public Map()
    {
        this( new BoundedGrid( 6, 50 ) );

        int count = 0;
        for ( int r = row - 2; r >= 0; r -= 2 )
        {
            if ( count % 2 == 0 )
            {
                for ( int c = 0; c < col - 1; c++ )
                {
                    this.add( new Location( r, c ), new Rock() );
                }
            }
            else
            {
                for ( int c = 1; c < col; c++ )
                {
                    this.add( new Location( r, c ), new Rock() );
                }
            }
            count++;
        }
        player = new PlayerCharacter( level );
        this.add( new Location( 5, 0 ), player );

        this.show();
        this.setMessage( "Use WASD to Move" );
        System.setProperty( "info.gridworld.gui.tooltips", "hide" );
    }


    public Map( BoundedGrid g )
    {
        super( g );
    }
    
    
    public void addMessage(String message)
    {
        this.setMessage( this.getMessage() + '\n' + message);
    }



    public boolean locationClicked( Location loc )
    {
        System.out.println( loc );
        return true;
    }


    public boolean keyPressed( String description, Location loc )
    {
        if ( description.equals( "D" ) )
        {
            player.moveRight();
        }
        if ( description.equals( "A" ) )
        {
            player.moveLeft();
        }
        if ( description.equals( "S" ) )
        {
            player.moveDown();
        }
        if ( description.equals( "W" ) )
        {
            player.moveUp();
        }

        if ( Math.random() < 0.3 )
        {
            enterCombat();
        }

        return true;
    }


    public void enterCombat()
    {
        updateLevel();
        c = new Combat( level );
        c.show();
        c.run();
    }


    public void updateLevel()
    {
        int cRow = player.getLocation().getRow() + 1;

        level = ( row - cRow ) / 2 + 1;
    }

}
